Java常用的强转类型,基本数据类型转换,JSONObject强转Map,JSONArray强转List(附源码)

您所在的位置:网站首页 java jsonarray转list Java常用的强转类型,基本数据类型转换,JSONObject强转Map,JSONArray强转List(附源码)

Java常用的强转类型,基本数据类型转换,JSONObject强转Map,JSONArray强转List(附源码)

2024-03-22 02:51| 来源: 网络整理| 查看: 265

Java常用的强转类型,基本数据类型转换,JSONObject强转Map,JSONArray强转List(附源码) 问题背景强制转换项目搭建总结Lyric: 无力的躺在干枯的河

问题背景

因为在项目常用一些强制转换,有时候忘了,记录一些常用的 注意事项:

可以下载源码进行参考 强制转换 强制转为Integer, 同类型编译器可以把对象直接转为基本数据类型 //常用的强制转换 int num = 100; String str = "50"; Integer intNum = Integer.valueOf(num); Integer intStr = Integer.valueOf(str); System.out.println(intNum); System.out.println(intStr); 强制转为long, 低类型可以直接向高类型转换,int可以之间转为long, 不需要强转 long longNum = (long) num; long longStr = Long.parseLong(str); System.out.println(longNum); System.out.println(longStr); 强制转为byte, 转为低类型,必须强制转换, 编译器可以把对象直接转为基本数据类型 byte byteNum = (byte) num; byte byteStr = Byte.parseByte(str); System.out.println(byteNum); System.out.println(byteStr); HashMap强制转为JSONObject HashMap hashMap = new HashMap(); hashMap.put("a", "b"); JSONObject jsonObject = new JSONObject(); jsonObject.putAll(hashMap); System.out.println(jsonObject); JSONObject强制转为HashMap JSONObject jsonObject1 = new JSONObject(); jsonObject1.put("a1", "b1"); HashMap hashMap1 = new HashMap(jsonObject1); System.out.println(hashMap1); Object的HashMap强制转为JSONObject HashMap hashMap2 = new HashMap(); hashMap2.put("a2", "b2"); Object mapTemp = hashMap2; if (mapTemp instanceof Map) { JSONObject jsonObject2 = new JSONObject(); HashMap temp = (HashMap) mapTemp; jsonObject2.putAll(temp); System.out.println(jsonObject2); } Object的JSONObject强制转为HashMap JSONObject jsonObject3 = new JSONObject(); jsonObject3.put("a3", "b3"); Object jsonTemp = jsonObject3; if (jsonTemp instanceof Map) { HashMap map2 = new HashMap((Map) jsonTemp); System.out.println(map2); } List强制转为JSONArray List list = new ArrayList(); list.add("a"); JSONArray jsonArray = new JSONArray(); jsonArray.addAll(list); System.out.println(jsonArray); JSONArray强制转为List JSONArray jsonArray1 = new JSONArray(); jsonArray1.add("a1"); List list1 = new ArrayList(jsonArray1); System.out.println(list1); Object的List强制转为JSONArray List list2 = new ArrayList(); list2.add("a2"); Object listObj = list2; if(listObj instanceof List) { JSONArray jsonArray2 = new JSONArray(); jsonArray2.addAll((List)listObj); System.out.println(jsonArray2); } Object的JSONArray强制转为List JSONArray jsonArray3 = new JSONArray(); jsonArray3.add("a3"); Object arry3 = jsonArray3; if(arry3 instanceof List) { List list3 = new ArrayList((List)arry3); System.out.println(list3); }

12 List类型通过stream连接成String类型

List list = new ArrayList(); list.add(1); list.add(2); String str = list.stream().map(Object::toString).collect(Collectors.joining(",")); 项目搭建

1 引入pom依赖

4.0.0 org.springframework.boot spring-boot-starter-parent 2.7.0 com.yg forceConvert 0.0.1-SNAPSHOT forceConvert Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.projectlombok lombok true com.alibaba fastjson 1.2.62 org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin org.projectlombok lombok

2 测试类

package com.yg.forceconvert; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import java.util.*; /** * @Author suolong * @Date 2022/6/1 21:36 * @Version 2.0 */ public class main { public static void main(String[] args) { //常用的强制转换 int num = 100; String str = "50"; //1. 强制转为Integer, 同类型编译器可以把对象直接转为基本数据类型 Integer intNum = Integer.valueOf(num); Integer intStr = Integer.valueOf(str); System.out.println(intNum); System.out.println(intStr); //2. 强制转为long, 低类型可以直接向高类型转换,int可以之间转为long, 不需要强转 long longNum = (long) num; long longStr = Long.parseLong(str); System.out.println(longNum); System.out.println(longStr); //3. 强制转为byte, 转为低类型,必须强制转换, 编译器可以把对象直接转为基本数据类型 byte byteNum = (byte) num; byte byteStr = Byte.parseByte(str); System.out.println(byteNum); System.out.println(byteStr); //4. HashMap强制转为JSONObject HashMap hashMap = new HashMap(); hashMap.put("a", "b"); JSONObject jsonObject = new JSONObject(); jsonObject.putAll(hashMap); System.out.println(jsonObject); //5. JSONObject强制转为HashMap JSONObject jsonObject1 = new JSONObject(); jsonObject1.put("a1", "b1"); HashMap hashMap1 = new HashMap(jsonObject1); System.out.println(hashMap1); //6. Object的HashMap强制转为JSONObject HashMap hashMap2 = new HashMap(); hashMap2.put("a2", "b2"); Object mapTemp = hashMap2; if (mapTemp instanceof Map) { JSONObject jsonObject2 = new JSONObject(); HashMap temp = (HashMap) mapTemp; jsonObject2.putAll(temp); System.out.println(jsonObject2); } //7. Object的JSONObject强制转为HashMap JSONObject jsonObject3 = new JSONObject(); jsonObject3.put("a3", "b3"); Object jsonTemp = jsonObject3; if (jsonTemp instanceof Map) { HashMap map2 = new HashMap((Map) jsonTemp); System.out.println(map2); } //8. List强制转为JSONArray List list = new ArrayList(); list.add("a"); JSONArray jsonArray = new JSONArray(); jsonArray.addAll(list); System.out.println(jsonArray); //9. JSONArray强制转为List JSONArray jsonArray1 = new JSONArray(); jsonArray1.add("a1"); List list1 = new ArrayList(jsonArray1); System.out.println(list1); //10. Object的List强制转为JSONArray List list2 = new ArrayList(); list2.add("a2"); Object listObj = list2; if(listObj instanceof List) { JSONArray jsonArray2 = new JSONArray(); jsonArray2.addAll((List)listObj); System.out.println(jsonArray2); } //11. Object的JSONArray强制转为List JSONArray jsonArray3 = new JSONArray(); jsonArray3.add("a3"); Object arry3 = jsonArray3; if(arry3 instanceof List) { List list3 = new ArrayList((List)arry3); System.out.println(list3); } } } 总结

可以快速使用强制转换

作为程序员第 147 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …

Lyric: 无力的躺在干枯的河


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3